package com.zjdiepu.www.biz.helper;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessVoMapper;
import com.zjdiepu.www.dao.base.BaseAccountBusiUnitMapper;
import com.zjdiepu.www.dao.base.SysUserMapper;
import com.zjdiepu.www.dao.other.OtherFundChannelConfigMapper;
import com.zjdiepu.www.dao.other.OtherWantWarningMapper;
import com.zjdiepu.www.dao.other.OtherWarningConfigurationMapper;
import com.zjdiepu.www.dao.system.BaseCustomerMapper;
import com.zjdiepu.www.dao.system.BaseSalersmanMapper;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.dao.zx.ZxQueryCreditApplySubMapper;
import com.zjdiepu.www.ibiz.other.IOtherWarningConfigurationService;
import com.zjdiepu.www.ibiz.zpp.AppNewsService;
import com.zjdiepu.www.model.base.dto.BaseAccountBusiUnit;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.other.dto.OtherMessageApp;
import com.zjdiepu.www.model.other.dto.OtherWantWarning;
import com.zjdiepu.www.model.zpp.pub.ext.ParamsExt;
import com.zjdiepu.www.model.zx.dto.ZxQueryCreditApplySub;
import com.zjdiepu.www.model.zx.ext.ZxQueryCreditApplySubExt;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 项目名称：diepu-parent-biz <br/> 
 * 类名称：ServiceHelper <br/>  
 * 类描述： biz帮助类 <br/>    
 * 创建人：xiongxiaotun <br/> 
 * 创建时间：2017年6月2日 下午1:17:42  <br/>   
 * 修改人：xiongxiaotun <br/> 
 * 修改时间：2017年6月2日 下午1:17:42   <br/>  
 * 修改备注：    <br/> 
 * @version V1.0   <br/>
 */
@Service
public class ServiceHelper {
	@Autowired
	private YwBusinessMapper ywBusinessMapper;
	@Autowired
	private YwBusinessVoMapper ywBusinessVoMapper;
	@Autowired
	private BaseCustomerMapper baseCustomerMapper;
	@Autowired
	private BaseAccountBusiUnitMapper baseAccountBusiUnitMapper;
	@Autowired
	private SysUserMapper sysUserMapper;
	@Autowired
	private AppNewsService appNewsService;
	@Autowired
	private ZxQueryCreditApplySubMapper zxQueryCreditApplySubMapper;
	@Autowired
	private OtherWantWarningMapper otherWantWarningMapper;
	@Autowired
	private BaseSalersmanMapper baseSalersmanMapper;
	@Autowired
	private OtherWarningConfigurationMapper otherWarningConfigurationMapper;
	@Autowired
	private OtherFundChannelConfigMapper otherFundChannelConfigMapper;
	@Autowired
	private IOtherWarningConfigurationService otherWarningConfigurationService;
	
	/**
	 * 检查身份证是否有重复
	 * @author xiongxiaotun <br/>
	 * @Title: checkIdNoDuplicate  <br/>
	 * @Description: 检查身份证是否有重复 <br/>
	 * @param queryCreditApplyId
	 * @return
	 * @version V1.0  <br/>
	 */
	public void checkIdNoDuplicate(String queryCreditApplyId){
		if(StringUtils.isBlank(queryCreditApplyId)) {
			return;
		}
		ZxQueryCreditApplySubExt zxQueryCreditApplySubExt = new ZxQueryCreditApplySubExt();
		zxQueryCreditApplySubExt.setBaseWhere();
		zxQueryCreditApplySubExt.withOutPage();
		zxQueryCreditApplySubExt.setQueryCreditApplyId(queryCreditApplyId);
		List<ZxQueryCreditApplySub> zxQueryCreditApplySubs = zxQueryCreditApplySubMapper.selectByExt(zxQueryCreditApplySubExt);
		if(CollectionUtils.isEmpty(zxQueryCreditApplySubs)) {
			return;
		}
		int size = zxQueryCreditApplySubs.size();
		outter:for(int i = 0; i < size; i++) {
			ZxQueryCreditApplySub thisZxQueryCreditApplySub = zxQueryCreditApplySubs.get(i);
			inner:for(int j = i + 1; j < size; j++) {
				ZxQueryCreditApplySub targetZxQueryCreditApplySub = zxQueryCreditApplySubs.get(j);
				String thisCusNo = thisZxQueryCreditApplySub.getCusNo();
				if(StringUtils.isBlank(thisCusNo)) {
					continue inner;
				}
				String targetCusNo = targetZxQueryCreditApplySub.getCusNo();
				if(StringUtils.isBlank(targetCusNo)) {
					continue inner;
				}
				if(thisCusNo.equals(targetCusNo)) {
					ExceptionHelper.thow("999", "客户【" + thisZxQueryCreditApplySub.getCusName() +  "】和【"  + targetZxQueryCreditApplySub.getCusName() +  "】身份证号码相同");
					break outter;
				}
			}
		}
	}
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: saveMsg  <br/>
	 * @Description: 保存消息 <br/>
	 * @param otherMessageApp
	 * @param salesmanId
	 * @param customerName
	 * @param shortMessage
	 * @param currentUser
	 * @param logger
	 * @version V1.0  <br/>
	 */
	public void saveMsg(OtherMessageApp otherMessageApp, String salesmanId, String customerName, String shortMessage, AbstractSysUser currentUser, Logger logger){
		//垫提车款成功
		try {
			otherMessageApp.setSalesmanId(salesmanId);
			ParamsExt paramsExt = new ParamsExt();
//			if(StringUtils.isBlank(salesmanId)) {
//				ExceptionHelper.thow("9999", "对应业务员ID为空");
//			}
			BaseAccountBusiUnit baseAccountBusiUnit = new BaseAccountBusiUnit();
			baseAccountBusiUnit.setIdUnit(salesmanId);
			baseAccountBusiUnit.notDeleted();
			BaseAccountBusiUnit dbBaseAccountBusiUnit = baseAccountBusiUnitMapper.selectRefined(baseAccountBusiUnit);
			if(dbBaseAccountBusiUnit != null && StringUtils.isNotBlank(dbBaseAccountBusiUnit.getUserId())) {
				String userId = dbBaseAccountBusiUnit.getUserId();
				SysUser sysUser = sysUserMapper.selectByPrimaryKey(userId);
				if(sysUser != null && StringUtils.isNotBlank(sysUser.getPhone())) {
					paramsExt.setMobile(sysUser.getPhone());
					paramsExt.setCode(shortMessage);
				}
			}
			paramsExt.setSalesmanId(salesmanId);
			appNewsService.saveAPPNews(otherMessageApp, paramsExt, currentUser);
		} catch (Exception e) {
			if(logger != null) {
				logger.error("保存消息/发送短信模块失败.{}",e);
			} else {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: saveAlert  <br/>
	 * @Description: 保存待处理预警信息 <br/>
	 * @param otherWantWarning
	 * @param otherMessageApp
	 * @param salesmanId
	 * @param customerName
	 * @param shortMessage
	 * @param currentUser
	 * @param logger
	 * @version V1.0  <br/>
	 */
	public void saveAlert(OtherWantWarning otherWantWarning, OtherMessageApp otherMessageApp, String salesmanId, String customerName, String shortMessage, AbstractSysUser currentUser){
		Map<String, Object> tcAlertMsgs = CollectionInitUtil.emptyMap();
		tcAlertMsgs.put("alertMsg", otherMessageApp);
		tcAlertMsgs.put("shortMsg", shortMessage);
		tcAlertMsgs.put("salesmanId", salesmanId);
		tcAlertMsgs.put("customerName", customerName);
		
		otherWantWarning.setContent(JSON.toJSONString(tcAlertMsgs));
		otherWantWarningMapper.insert(otherWantWarning);
	}
}
